package aaaFace;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.json.JSONObject;

import com.attilax.io.filex;
import com.attilax.json.AtiJson;
import com.facepp.error.FaceppParseException;
import com.facepp.http.HttpRequests;
import com.facepp.http.PostParameters;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class t {
	
	
	

	public static void main(String[] args) throws IOException, FaceppParseException {
		
		
		String imgpath = "e:\\122155TP4.jpg";
		imgpath = "e:\\94f6d7e04a4d452035300f18b984988c.jpg";
		
		String faces_json = getFacesJson(imgpath);
		System.out.println(faces_json);
		File _file = new File(imgpath);
		   Image src = ImageIO.read(_file);
		    int width = src.getWidth(null);
		    int height = src.getHeight(null);
		int w=width;
		int h=height;
		Double left_x = null;
		Double left_y = null;
	String t=faces_json;
			//filex.read("e:\\x.json");
	t=t.replace("\n", "");t=t.replace("\r", "");t=t.replace("\t", "");
	Map m =AtiJson.fromJson(t);
	List<Map> li=(List) m.get("face");
	for (Map face : li) {
		Map position=(Map) face.get("position");
		Map mouth_left=(Map) position.get("mouth_left");
		  left_x= ((Double)mouth_left.get("x")/100)*w;
		  left_y=((Double)mouth_left.get("y")/100)*h;
		System.out.println("mouth_left:"+Math.round(left_x) +","+Math.round(left_y));
	}
	if(li.size()>0)
	  printPosintPic(imgpath,   left_x, left_y);
	System.out.println("--f");

	}

	private static void printPosintPic(String imgpath , Double left_x, Double left_y)
			throws FileNotFoundException, IOException {
		// 初始化被写水印的图片
		
    Image src = ImageIO.read(new File(imgpath));
    int width = src.getWidth(null);
    int height = src.getHeight(null);
		// 创建图片缓冲区
		BufferedImage image = new BufferedImage(width, height,
		  BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.drawImage(src, 0, 0, width, height, null);
   // g.drawRect(x, y, width, height);
		g.drawOval(left_x.intValue(),left_y.intValue(),5,5); //只要使w=h即可画圆 
		g.dispose();
		
		
		String targetImg="e:\\"+filex.getUUidNameBaseOriname(imgpath)+".jpg";
		// 将缓冲区中的信息写入图片
		FileOutputStream out = new FileOutputStream(targetImg);
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(image);
		out.close();
	}

	private static String getFacesJson(String imgpath)
			throws FaceppParseException {
		String f=imgpath;
		//replace api_key and api_secret here (note)
		HttpRequests httpRequests = new HttpRequests("4480afa9b8b364e30ba03819f3e9eff5", "Pz9VFT8AP3g_Pz8_dz84cRY_bz8_Pz8M ", true, true);
		System.out.println("FacePlusPlus API Test:");
		
		//detection/detect
		PostParameters postParameters = new PostParameters();
		postParameters.setImg(new File(f));
		//postParameters.setUrl("http://cn.faceplusplus.com/wp-content/themes/faceplusplus/assets/img/demo/9.jpg")
		JSONObject result = httpRequests.detectionDetect(postParameters);
		
		String faces_json=result.toString();
		return faces_json;
	}

}
